package com.tozed.test;

import com.tozed.util.Util;
import com.tozed.widget.TitleBar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class SDCardTestActivity extends Base implements OnClickListener
{
	private static final String TAG = "SDCardTestActivity";

	private TitleBar mTitleBar;
	private TextView mTextInfo;
	private TextView mTextContent;
	private Button mBtnPass, mBtnFail;
	
	private Button mBtnCheckSDCard;
	
	private int mTestType = Constant.TYPE_TEST_SINGLE;
	
	private final static int MSG_AUTO_TEST_START = 1;
	private final static int MSG_AUTO_TEST_SUCC = 2;
	private final static int MSG_AUTO_TEST_FAIL = 3;
	
	private Handler mHandler = new Handler() {
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_AUTO_TEST_START:
				new Handler().postDelayed(new Runnable(){	 
					public void run() {    
					//execute the task	 
					mBtnCheckSDCard.performClick();
					}	 
				 }, 1000); 
				break;
			case MSG_AUTO_TEST_SUCC:
				new Handler().postDelayed(new Runnable(){	 
					public void run() {    
					//execute the task	 
					mBtnPass.performClick();
					}	 
				 }, 1000); 
				break;
			case MSG_AUTO_TEST_FAIL:
				new Handler().postDelayed(new Runnable(){	 
					public void run() {    
					//execute the task	 
					mBtnFail.performClick();
					}	 
				 }, 1000);
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_sdcard);
		
		setupViews();

		Intent intent = getIntent();
		if (intent != null)
			mTestType = intent.getIntExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
		if (mTestType == Constant.TYPE_TEST_AGING)
			mHandler.sendEmptyMessage(MSG_AUTO_TEST_START);
	}
	
	private void setupViews(){
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.str_test_sdcard);
		mTitleBar.hideRightButton();
		
		mBtnPass = (Button)findViewById(R.id.btn_pass);
		mBtnFail = (Button)findViewById(R.id.btn_fail);
		mBtnPass.setOnClickListener(this);
		mBtnFail.setOnClickListener(this);
		mBtnPass.setEnabled(false);
		
		mTextInfo = (TextView)findViewById(R.id.text_tip);
		mTextContent = (TextView)findViewById(R.id.text_content);
		
		mBtnCheckSDCard = (Button)findViewById(R.id.btnCheckSDCard);
		mBtnCheckSDCard.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				checkSDCard();
			}
		});
	}
	
	private void checkSDCard()
	{
		boolean internal_tflash_successfully = false;
		boolean external_tflash_successfully = false;

		Log.v(TAG, "sdcard");
		StringBuilder message = new StringBuilder();
		Context context = SDCardTestActivity.this;

		long internalTotal = Util.getInternalTFlashCapability(context);
		Log.v(TAG, "sdcard sdcard(0) internalTotal = " + internalTotal);
		if (internalTotal > 0) {
			long imb = internalTotal / (1024 * 1024);
			message.append(getString(R.string.str_check_internal_sdcard_successfully)).append(imb).append("MB");
			internal_tflash_successfully = true;
		} else {
			message.append(getString(R.string.str_check_internal_sdcard_failed));
		}

		long externalTotal = Util.getExternalTFlashCapability(context);

		Log.v(TAG, "sdcard sdcard(1) externalTotal = " + externalTotal);
		if (externalTotal > 0) {
			long emb = externalTotal / (1024 * 1024);
			message.append("\n").append(getString(R.string.str_check_external_sdcard_successfully)).append(emb).append("MB");
			if (internal_tflash_successfully) {
				Log.v(TAG, "sdcard double check successfully");
				mBtnPass.setEnabled(true);
				//if (mTestType == Constant.TYPE_TEST_AGING) {
				//	mBtnPass.performClick();
				//}
				external_tflash_successfully = true;
			}
		}
		else {
			message.append("\n").append(getString(R.string.str_check_external_sdcard_failed));
		}

		Log.v(TAG, "sdcard message = " + message.toString());
		mTextContent.setText(message.toString());

		if (mTestType == Constant.TYPE_TEST_AGING){
			if (internal_tflash_successfully && external_tflash_successfully) {
				mHandler.sendEmptyMessage(MSG_AUTO_TEST_SUCC);
			}
			else {
				mHandler.sendEmptyMessage(MSG_AUTO_TEST_FAIL);
			}
		}
	}
	
	
	@Override
	public void onBackPressed() {
	    if (mTestType == Constant.TYPE_TEST_AGING) {
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent);
	    }
	    
	    super.onBackPressed();
	}
	 
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_pass:
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 1);
			setResult(RESULT_OK, intent);
			finish();
			break;
			
		case R.id.btn_fail:
			Intent intent2 = new Intent();
			intent2.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent2);
			finish();
			break;
			
		default:
			break;
		}
	}
}
